﻿/*
    Copyright (C) 2008, wj2929, wj2929@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    *KEEP THIS HEADER INFORMATION*
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Net;

namespace mjRegister.BAL
{
    //验证码
    public class VerifyCode
    {
        public string rcode = string.Empty;
        public CookieCollection cc = new CookieCollection();

        //获取验证码
        public Image GetVerifyCodeImage()
        {
            Random r = new Random();
            rcode = r.NextDouble().ToString();
            HttpProc hp = new HttpProc();
            Image img = null;
            hp.strUrl = string.Format("http://www.kaixin001.com/interface/regcreatepng.php?randnum={0}", rcode);
            HttpWebRequest 请求 = hp.CreateRequest();
            HttpWebResponse 响应 = null;
            try
            {
                响应 = (HttpWebResponse)请求.GetResponse();
                img = Image.FromStream(响应.GetResponseStream());
                if (响应.Cookies.Count > 0)
                {
                    cc = 响应.Cookies;
                }
                if (img != null)
                {
                    return img;
                }
                else
                    throw new Exception("不能获取验证码。");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        //判断验证码是否正确
        public bool CheckCode(string code)
        {
            string url = "http://www.kaixin001.com/interface/checkcode.php";
            string postData = string.Format("code={0}&rcode={1}", code, rcode);
            HttpProc hp = new HttpProc(url, postData, cc);
            return hp.Proc() == "1";
        }
    }
}
